using CustomerManagementSystem.Domain.Entities;
using CustomerManagementSystem.Domain.Repositories;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CustomerManagementSystem.Application;

public class CustomerService : ICustomerService
{
    private readonly IRepository<Customer> _customerRepository;
    private readonly IAuthService _authService;

    public CustomerService(IRepository<Customer> customerRepository, IAuthService authService)
    {
        _customerRepository = customerRepository;
        _authService = authService;
    }

    public async Task<IEnumerable<Customer>> GetAllCustomersAsync()
    {
        return await _customerRepository.GetAllAsync();
    }

    public async Task<Customer?> GetCustomerByIdAsync(Guid id)
    {
        return await _customerRepository.GetByIdAsync(id);
    }

    public async Task<Customer> CreateCustomerAsync(Customer customer)
    {
        if (!string.IsNullOrEmpty(customer.PasswordHash))
        {
            customer.PasswordHash = _authService.HashPassword(customer.PasswordHash);
        }
        return await _customerRepository.CreateAsync(customer);
    }

    public async Task UpdateCustomerAsync(Customer customer)
    {
        await _customerRepository.UpdateAsync(customer);
    }

    public async Task DeleteCustomerAsync(Guid id)
    {
        var customer = await _customerRepository.GetByIdAsync(id);
        if (customer != null)
        {
            await _customerRepository.DeleteAsync(customer);
        }
    }
}
